package com.yztc.orangeentertainment.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.yztc.orangeentertainment.App;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.bean.UserBean;
import com.yztc.orangeentertainment.ui.login.phonelogin.PhoneLoginActivity;
import com.yztc.orangeentertainment.ui.main.MainActivity;
import com.yztc.orangeentertainment.utils.CircleImageView;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by My on 2016/10/15.
 * 登录页面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.login_wx)
    CircleImageView loginWx;
    @InjectView(R.id.login_qq)
    CircleImageView loginQq;
    @InjectView(R.id.login_sina)
    CircleImageView loginSina;
    @InjectView(R.id.login_phone)
    CircleImageView loginPhone;
    @InjectView(R.id.tv_back)
    TextView tvBack;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
        initShow();
    }

    /**
     * 舒适化数据
     */
    private void initShow() {
        loginPhone.setOnClickListener(this);
        loginQq.setOnClickListener(this);
        loginSina.setOnClickListener(this);
        loginWx.setOnClickListener(this);
        tvBack.setOnClickListener(this);
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_phone:
                //手机登录
                Intent intent=new Intent(LoginActivity.this, PhoneLoginActivity.class);

                startActivity(intent);
                finish();
                break;
            case R.id.login_sina:
                //新浪登录
                Platform platform=ShareSDK.getPlatform(SinaWeibo.NAME);
                platform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
                        Log.i("tag","授权成功");
                        //判断是否获取到用户信息
                        if(action==platform.ACTION_USER_INFOR){
                            //获取数据平台数据
                            PlatformDb platformDb=platform.getDb();
                            // db对象获取用户数据
                            platformDb.getToken();//登录令牌
                            platformDb.getUserIcon();//获取用户图片
                            String userId=platformDb.getUserId();//获取当前第三方登录的用户id
                            String userName=platformDb.getUserName();//获取第三方用户名称
                            Log.i("tag","UserId="+userId+",UserName="+userName);
                        }
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.i("tag","登录失败!");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.i("tag","登录取消!");
                    }
                });
                platform.authorize();
                platform.showUser(null);
                break;
            case R.id.login_qq:
                //qq登录
                platform=ShareSDK.getPlatform(QQ.NAME);
                platform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
                        Log.i("tag","授权成功");
                        //判断是否获取到用户信息
                        if(action==platform.ACTION_USER_INFOR){
                            //获取数据平台数据
                            PlatformDb platformDb=platform.getDb();
                            // db对象获取用户数据
                            platformDb.getToken();//登录令牌
                            platformDb.getUserIcon();//获取用户图片
                            String userId=platformDb.getUserId();//获取当前第三方登录的用户id
                            String userName=platformDb.getUserName();//获取第三方用户名称
                            UserBean userBean=new UserBean();
                            userBean.setId(userId);
                            userBean.setUserName(userName);
                            userBean.setSex(platformDb.getUserGender());
                            userBean.setPsw("123456");
                            userBean.setUserPicture(platformDb.getUserIcon());
                            App.getLiteOrm().insert(userBean);
                            Intent intent=new Intent();
                            intent.putExtra("id",userId);
                            //setResult(表示结果码 标示结果 >0的整数,表示存储传递回传数据的intent )
                            //哪个activity调用startActivityForResult()启动该activity  那么setResult()就回传给那个activity
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.i("tag","登录失败!");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.i("tag","登录取消!");
                    }
                });
                platform.authorize();
                platform.showUser(null);
                break;
            case R.id.login_wx:
                //微信登录
                platform=ShareSDK.getPlatform(Wechat.NAME);
                platform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
                        Log.i("tag","授权成功");
                        //判断是否获取到用户信息
                        if(action==platform.ACTION_USER_INFOR){
                            //获取数据平台数据
                            PlatformDb platformDb=platform.getDb();
                            // db对象获取用户数据
                            platformDb.getToken();//登录令牌
                            String userId=platformDb.getUserId();//获取当前第三方登录的用户id
                            String userName=platformDb.getUserName();//获取第三方用户名称
                            UserBean userBean=new UserBean();
                            userBean.setId(userId);
                            userBean.setUserName(userName);
                            userBean.setSex(platformDb.getUserGender());
                            userBean.setPsw("123456");
                            userBean.setUserPicture(platformDb.getUserIcon());
                            App.getLiteOrm().insert(userBean);
                            Intent intent=new Intent();
                            intent.putExtra("id",userId);
                            //setResult(表示结果码 标示结果 >0的整数,表示存储传递回传数据的intent )
                            //哪个activity调用startActivityForResult()启动该activity  那么setResult()就回传给那个activity
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.i("tag","登录失败!");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.i("tag","登录取消!");
                    }
                });
                platform.authorize();
                platform.showUser(null);
                break;
            case R.id.tv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent=new Intent();
        intent.putExtra("str", "我是回传数据");
        //setResult(表示结果码 标示结果 >0的整数,表示存储传递回传数据的intent )
        //哪个activity调用startActivityForResult()启动该activity  那么setResult()就回传给那个activity
        setResult(Activity.RESULT_OK, intent);
        this.finish();
    }
}
